/**
 * Created by Administrator on 2018/7/17.
 */
import axios from 'axios'
import { Toast } from 'mint-ui'
import router from '../router/index'
var RUN_MODE = 0
export const BASE_URL = [
  'http://xjdemo.cnsaas.com/edu-app/'
  // 'http://192.168.2.49:8080'
  // 'http://192.168.2.60:8089'
  // 'http://192.168.40.25:8080'
  /* 'http://appcloud-dev-gateway.cnsaas.com/', // 开发环境
  'http://wow-gateway-dev.cnsaas.com/', // 测试环境
  'http://223.220.252.167:9080/', // UAT环境
  'http://223.220.252.167:8300/'  // 生产环境 */
]
axios.defaults.timeout = 10000
axios.defaults.baseURL = ''

//http request 拦截器
axios.interceptors.request.use(
  config => {
    //在发送请求之前做某事
    // const token = getCookie('名称');注意使用的时候需要引入cookie方法，推荐js-cookie
    // const token = JSON.parse(window.localStorage.getItem('access_token'))
    config.data = JSON.stringify(config.data)
    config.headers = {
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json;charset=utf-8',
      'Authorization': `${localStorage.getItem('access_token')}`
    }
    // if(token){
    //   config.params = {'Authorization': token}
    // }
    return config
  },
  error => {
    //请求错误时做些事
    return Promise.reject(error)
  }
)

//http response 拦截器
axios.interceptors.response.use(
  response => {
    //对响应数据做些事
     /*if (response.data.code == 2) {
       router.replace({
         path:"/login",
         query: { redirect: router.currentRoute.fullPath }//从哪个页面跳转
       })
    } */

    return response.data
  },
  error => {
    //请求错误时做些事
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = '错误请求'
          break
        case 401:
          error.message = '未授权，请重新登录'
          break
        case 403:
    /*      error.message = '拒绝访问'*/
          error.message = '账号登录信息失效，请重新登录'
          window.localStorage.removeItem('access_token')
          window.localStorage.removeItem('userInfo')
          router.replace({
            path:"/login",
            query: { redirect: router.currentRoute.fullPath }//从哪个页面跳转
          })
          break
        case 404:
          error.message = '请求错误,未找到该资源'
          break
        case 405:
          error.message = '请求方法未允许'
          break
        case 408:
          error.message = '请求超时'
          break
        case 500:
          error.message = '服务器端出错'
          break
        case 501:
          error.message = '网络未实现'
          break
        case 502:
          error.message = '网络错误'
          break
        case 503:
          error.message = '服务不可用'
          break
        case 504:
          error.message = '网络超时'
          break
        case 505:
          error.message = 'http版本不支持该请求'
          break
        default:
          error.message = `连接错误${error.response.status}`
      }
    } else {
      error.message = '连接到服务器失败'
    }
    Toast(error.message)
    return Promise.reject(error)
  }
)
export default {
  getUrl (url) {
    return BASE_URL[RUN_MODE] + url
  },
  request (method, url, request) {
    return new Promise((resolve, reject) => {
      axios({
        method: method,
        url: this.getUrl(url),
        data: request ? request.data : null,
        params: request ? request.params : null
      }).then(response => {
        resolve(response)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /* 获取后端传入的api地址（url不用拼接） */
  weiApi (method, url, request) {
    return new Promise((resolve, reject) => {
      axios({
        method: method,
        url: url,
        data: request ? request.data : null,
        params: request ? request.params : null
      }).then(response => {
        resolve(response)
      }).catch(err => {
        reject(err)
      })
    })
  },
  addCookie (method, url, request, Cookie) {
    return new Promise((resolve, reject) => {
      axios({
        method: method,
        url: url,
        data: request ? request.data : null,
        params: request ? request.params : null,
        headers:{
          'Access-Control-Allow-Origin': '*',
          'Content-Type': 'application/json;charset=utf-8',
          'Cookie': Cookie
        },
        withCredentials: true
      }).then(response => {
        resolve(response)
      }).catch(err => {
        reject(err)
      })
    })
  }
}
